package com.probiz.estore.catalog.service.impl;

import java.util.List;

import com.probiz.estore.catalog.service.ContentCategoryManager;
import com.probiz.estore.catalog.service.ContentCategoryNtManager;
import com.probiz.estore.common.model.catalog.ContentCategory;
import com.probiz.estore.content.service.ContentManager;
import com.probiz.estore.core.search.SearchCriteria;

public class ContentCategoryNtManagerImpl implements ContentCategoryNtManager {
	
	private ContentCategoryManager contentCategoryManager;
	
	private ContentManager	contentManager;

	
	public void setContentCategoryManager(
			ContentCategoryManager contentCategoryManager) {
		this.contentCategoryManager = contentCategoryManager;
	}


	public void setContentManager(ContentManager contentManager) {
		this.contentManager = contentManager;
	}


	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.service.contentCategoryNtManager#calItemCountForScheduling()
	 */
	public void calItemCountForScheduling() {
		String hql = "from ContentCategory cc";
		boolean flag =true;
		int pageSize = 10;
		int pageNo = 1;
		do{
			SearchCriteria sc = SearchCriteria.getHqlPagingInstance(hql,null,pageNo,pageSize,null);
			List<ContentCategory> contentCategoryList = contentCategoryManager.searchByCriteria(sc);
	        for (ContentCategory contentCategory : contentCategoryList) {
	        	try{
	        		Integer itemCount = contentManager.countContentIncludeAllByCategory(contentCategory);
	        		Integer activeItemCount = contentManager.countActiveContentIncludeAllByCategory(contentCategory);
	        		contentCategory.setItemCount(itemCount);
	        		contentCategory.setActiveItemCount(activeItemCount);
	        		contentCategoryManager.save(contentCategory);
	        	}catch(Exception e){
	        		e.printStackTrace();
	        	}
	        }
			if(contentCategoryList.size()!=pageSize){
				flag=false;
			}else{
				++pageNo;
			}
		}while(flag);	
		
		
	}

}
